package org.example.mall.Controller;

import org.example.mall.Pojo.Order;
import org.example.mall.Pojo.OrderDetail;
import org.example.mall.Pojo.Result;
import org.example.mall.Service.OrderService;
import org.example.mall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService oService;

    @PostMapping("/new")
    String createOrder(@RequestBody Order order) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = map.get("username").toString();
//        String username = "user02";
        Integer cId = oService.createOrder(username,order.getPId(),order.getPrice(), order.getCId());
        return (cId).toString();
    }
    @GetMapping("/delete")
    void deleteOrder(@RequestParam("oid") Integer oid) {
        oService.deleteOrder(oid);
    }
    @GetMapping("/list")
    List<OrderDetail> getOrderList() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = map.get("username").toString();
//        String username = "user02";
        return oService.getOrderListByUserName(username);
    }
    @GetMapping("/detail")
    OrderDetail getOrderDetail(@RequestParam("oid") Integer oid) {
        return oService.getOrderDetailByOId(oid);
    }

    @GetMapping("/complete")
    void orderClose(@RequestParam("oid") Integer oid) {
        oService.completeOrder(oid);
    }

    @GetMapping("/cancel")
    void orderCancel(@RequestParam("oid") Integer oid) {
        oService.cancelOrder(oid);
    }
    @GetMapping("/pay")
    void tmp_paied(@RequestParam("oid") Integer oid) {
        oService.payOrder(oid);
    }
}
